package kyuuri.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StreamInfo extends HttpServlet {
	final String STREAM_INFO_URL = "http://live.nicovideo.jp/api/getstreaminfo/";
	private final static long serialVersionUID = 2;
	@Override
	protected void doGet(HttpServletRequest req, final HttpServletResponse resp)
			throws ServletException, IOException {

		try {
			resp.setContentType("application/xml");
			resp.setCharacterEncoding("UTF-8");
			URL url = new URL(STREAM_INFO_URL + req.getParameter("guid"));
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(url.openStream(), "UTF-8"));
			PrintWriter out;
			out = resp.getWriter();
			char buf[] = new char[100000];
			int length = reader.read(buf, 0, 100000);
			out.write(buf, 0, length);
			out.flush();
		} catch (IOException e) {
			// ...
		}
	}
}